const COLUMNS = [{
		prop: "type",
		label: "类型",
		width: "80"
	},{
		prop: "stype",
		label: "小类型",
		width: "80"
	},
	{
		prop: "questionType",
		label: "题型",
		width: "80"
	},

	{
		prop: "tiMu",
		label: "题目",
		width: "200"
	},
	{
		prop: "rightchoose",
		label: "正确选择",
		width: "70"
	},
	{
		prop: "addTime",
		label: "添加时间",
		width: "120"
	},
	{
		prop: "updateTime",
		label: "更新时间",
		width: "120"
	}
]


let app = new Vue({
	el: "#app",
	
	data() {
		return {
			saverules: {
				type: [{
					required: true,
					message: '请输入类型名称',
					trigger: 'blur'
				}, ],
				questionType: [{
					required: true,
					message: '请输入题型名称',
					trigger: 'blur'
				}],
				tiMu: [{
					required: true,
					message: '请输入题目名称',
					trigger: 'blur'
				}]
			},
			typeoptions: [],
			stypeoptions:[],
			typealloptions:[],
			questionTypeoptions: MYQUESTIONTYPE,
			leftbar_show: true,
			// 添加、编辑标识
			tableHeight: window.innerHeight - 180,
			tablecolums: COLUMNS,
			submitDisable: false,
			dialogFormVisible: false,
			dialogEditFormVisible: false,
			questionbankForm: {
				xuanXiang: [{
					"label": "",
					"text": ""
				}, ],
				action: "",
				type: "",
				stype: [],
				questionType: "",
				tiMu: "",
				rightchoose: [{
					value: ''
				}],
			},
			queryForm: {
				type: "",
				stype: [],
				tiMu: "",
				questionType: "",
				addTime: [],
				updateTime: [],
				pageNo: 1, // 当前页码
				pagesize: 100, // 每页记录数
				pagesizes: [10, 20, 50, 100]
			},

			pagetotal: 0,
			pagetotaltemp: 0,
			pickerOptions: {
				shortcuts: [{
					text: '最近一周',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
						picker.$emit('pick', [start, end]);
					}
				}, {
					text: '最近一个月',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
						picker.$emit('pick', [start, end]);
					}
				}, {
					text: '最近三个月',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
						picker.$emit('pick', [start, end]);
					}
				}]
			},
			// 多选
			multipleSelection: [],
			pagedatatemp: [], // 当前页结果集
			pagedata: [], // 当前页结果集
			loaded: false,
		}
	},
	methods: {
		changetype() {
			if (this.questionbankForm.questionType == "单选") {
				rightchoose = [{
					value: ''
				}]
				this.$set(this.questionbankForm, "rightchoose", rightchoose)
			}
		},
		removeitem(item) {
			var index = this.questionbankForm.rightchoose.indexOf(item)
			if (index !== -1) {
				this.questionbankForm.rightchoose.splice(index, 1)
			}
		},
		additem() {
			this.questionbankForm.rightchoose.push({
				value: '',
				key: Date.now()
			});
			console.log(this.questionbankForm)
		},
		addlastitems(index, type) {
			if (type === '1') {
				const lastitem = this.questionbankForm.xuanXiang[this.questionbankForm.xuanXiang.length - 1].label
				if (lastitem != undefined) {
					if (lastitem.trim() === '') {
						this.$message.error('请您填写完一项后继续追加')
					} else {
						this.questionbankForm.xuanXiang.push({
							"label": '',
							"text": '',
						})

					}
				}

			}
		},
		rmlastitems(index, type) {
			console.log(this.questionbankForm)
			switch (type) {
				case '1':
					this.questionbankForm.xuanXiang.splice(index, 1)
					break
				default:
					break
			}
			this.$message.success('移除成功')
		},




		toolAction(acName) {
			if (acName == "添加") {
				this.add()
			}
		},

		tableRowClassName({
			row,
			rowIndex
		}) {
			if (this.pagedata[rowIndex].perfect_flag == '已完善') {
				return 'success-row';
			}
			return '';
		},


		// 添加
		add() {
			this.dialogFormVisible = true;
			this.questionbankForm = {
				xuanXiang: [{
					"label": "",
					"text": "",
				}],
				type: "",
				stype: [],
				questionType: "",
				tiMu: "",
				rightchoose:[{
					value: ''
				}],
				action: "add",
			}
		},


		// 显示侧边栏
		showLeftBar() {

			if (this.leftbar_show)
				return this.leftbar_show = false
			if (!this.leftbar_show)
				return this.leftbar_show = true

		},
		// 表格多选 
		handleSelectionChange(val) {
			this.multipleSelection = val;
		},
		handleRemove(file, filelist) {},

		handleAvatarSuccess(res, file, filelist) {},



		// 检查数据是否有修改
		checkDataChanged() {
			// 比对数据有无发生变化
			for (let key in this.questionbankForm) {
				if (this.questionbankForm[key] != this.questionbankFormOrg[key]) {
					return true;
				}
			}
			return false;
		},
		// 确认
		async confirm() {
			this.submitDisable = true;
			this.$refs.questionbankForm.validate(async valid => {
				if (valid) {
					const loading = this.$loading(LOAD_STYLE);
					if (this.questionbankForm.questionType == "简答") {
						this.questionbankForm.xuanXiang = [{
							"text": ""
						}];
						this.questionbankForm.rightchoose = "";
					}
					try {
						let res = null;
						if (this.questionbankForm.action == "add") {
							//添加
							res = await axios.put('api/questionbankview/add', this.questionbankForm);

						} else {
							//编辑
							res = await axios.put('api/questionbankview/edit', this.questionbankForm);

						}
						if (res.code === 200) {
							this.initTestTypes();
							this.initTestSubTypes("None");

							this.dialogFormVisible = false
							this.query()
							this.$message.success(res.message);
						} else {
							this.$message.error(res.message);
						}

					} catch (e) {
						this.$message.error('请求出错！', e.message);
					}

					// 关闭加载框
					loading.close();
				}
			});
			this.submitDisable = false;
		},
		start() {
			this.queryForm.pageNo = 1
			this.query()
		},

		// 查询
		async query() {
			this.submitDisable = true;
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.post('api/questionbankview/querylist', this.queryForm);
				if (res.code === 200) {
					this.pagedata = res.data;
					this.pagetotal = res.total;
					for (let item in this.pagedata) {
						this.pagedata[item].addTime = this.pagedata[item].addTime
						//console.log(this.pagedata[item].perfect_flag)

					}
					this.$message.success(res.message);

				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('请求出错！', e.message);
			}

			// 关闭加载框
			loading.close();
			this.submitDisable = false;
		},


		selectionChange(selection) {
			this.selection = selection
		},


		// 编辑
		edit(index, row) {

			this.index = index;
			this.dialogFormVisible = true;
			this.questionbankForm = deepCopy(row)
			this.questionbankForm.action = "edit"
			this.questionbankForm.rightchoose = this.questionbankForm.rightchoose.replace(/'/g, "")
			var arr = this.questionbankForm.rightchoose.split(",");
			var srr = [];
			for (let index = 0; index < arr.length; index++) {
				srr.push({
					value: arr[index],
					key: Date.now()
				})
			}
			this.$set(this.questionbankForm, "rightchoose", srr)


			// 保留原始数据，方便比对
			this.questionbankFormOrg = deepCopy(row)
			this.questionbankFormOrg.action = "edit"
		},


		// 设定表格当前页
		handleCurrentPageChange(selectedPageNo) {
			this.queryForm.pageNo = selectedPageNo
			// 加载数据页
			this.query()
		},
		//设定每页记录数
		handleSizeChange(val) {
			this.queryForm.pagesize = val
			// 加载数据页
			this.query()
		},


		createStateFilter(queryString) {
			return (state) => {
				return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
			};
		},
		handleSelect(item) {
			console.log(item);
		},

		async initTestTypes() {
			try {
				let res = await axios.get('api/test/inittype');
				if (res.code === 200) {
					this.typeoptions = res.data
				} else {
					this.$toast("题库大分类取得出错。");
				}
			} catch (e) {
				this.$toast("网络连接异常。");
			}
		},
		async initTestSubTypes(type) {
			try {
				let res = await axios.get('api/test/initsubtype/' + type);
				if (res.code === 200) {
					this.stypeoptions = res.data
				} else {
					this.$toast("题库小分类取得出错。");
				}
			} catch (e) {
				this.$toast("网络连接异常。");
			}
		},
		changeType(value) {
			this.initTestSubTypes(value);
		}

	},

	async mounted() {
		// 自动调整表格高度
		window.onresize = () => {
			return (() => {
				this.tableHeight = window.innerHeight - 180
			})()
		}
		this.initTestTypes();
		this.initTestSubTypes("None");
	},


});
